import shutil

from tools.view import *


class MainWindowModel:
    def __init__(self, master):
        self.master = master
        # self.menu_ls = []
        self.module_ls = []
        self.model_ls = []

        pass

    def sec_init(self):
        # self.menu_ls = ["模块", "模型", "操作"]
        self.module_ls = [Module.Backtesting, Module.FactorAnalysis, Module.Fighting, Module.KChart,
                          Module.DataBridge, Module.MachineLearning]
        model_dir_path = os.path.join(self.master.file_manager.project_path, "model")

        self.model_ls = [f for f in os.listdir(model_dir_path) if os.path.isdir(os.path.join(model_dir_path, f)) and "model" in f]

    def on_change_module_name(self, module: Module):
        from core.map_obj import ModuleModelMap
        # 修改file_manager中当前动态模块
        self.master.file_manager.change_now_module(module)
        # 实例化动态模块实体类
        self.master.dynamic_module_model_dc[self.master.file_manager.now_dynamic_module] = ModuleModelMap[self.master.file_manager.now_dynamic_module](self.master)  # 获取后实例化
        self.master.dynamic_module_model_dc[self.master.file_manager.now_dynamic_module].sec_init()
        # 清空之前模块内容
        for widget in self.master.main_window_view.dynamic_module_frame.winfo_children():
            widget.destroy()
        # 显示 动态模块 窗口
        self.master.main_window_view.layout_dynamic_module_view()
        # 清空running中canvas和text的内容
        self.master.main_window_view.init_running()

    def on_change_model_name(self, model_name):
        if self.master.file_manager.run_status.is_run:
            info = "当前存在运行中的任务，无法切换模型"
            self.master.file_manager.log_engine.emit(info, LogName.Running)
            return
        from core.map_obj import ModuleModelMap
        # 同步file_manager中的相关操作
        self.master.file_manager.change_now_model(model_name)
        # 实例化动态模块实体类
        self.master.dynamic_module_model_dc[self.master.file_manager.now_dynamic_module] = ModuleModelMap[self.master.file_manager.now_dynamic_module](self.master)  # 获取后实例化
        self.master.dynamic_module_model_dc[self.master.file_manager.now_dynamic_module].sec_init()
        # 实例化动态模块视图类
        self.master.main_window_view.layout_dynamic_module_view()
        # 清空running中canvas和text的内容
        self.master.main_window_view.init_running()

    def gen_model(self):
        # 获取当前model下非孢子模板的model包中最大的编号，忽略非一般格式的model
        norm_model_num_ls = [int(f.split("model")[1]) for f in self.model_ls if f.split("model")[1].isdigit()]
        max_num = max(norm_model_num_ls)
        new_model_num = max_num + 1
        new_model_name = "model"+str(new_model_num)
        # 重命名modelx后复制到新的文件夹，实现模型生成

        # 原始文件夹路径
        spore_model_path = os.path.join(self.master.file_manager.project_path, "model", "spore")

        # 目标文件夹路径（复制到的位置，与原始文件夹在同一目录下）
        new_model_path = os.path.join(os.path.dirname(spore_model_path), new_model_name)

        # 复制文件夹
        shutil.copytree(spore_model_path, new_model_path)

        info = f"新模型{new_model_name}已生成，路径：{new_model_path}"
        self.master.file_manager.log_engine.emit(info, LogName.Operate)
        # 刷新下模型下的菜单项
        self.sec_init()
        # 更新view下的model菜单项
        self.master.main_window_view.update_menu()

        pass

    def factor_library_report(self):
        # 附加功能因子库报告，用于给出当前model下的因子画像

        pass

    def data_merge(self):
        # 数据合并功能，用于给选定的data进行周期从小到大的合并操作，并提供时间截取操作
        from module.addition_module.data_merge.model import DataMergeModel
        data_merge_model = DataMergeModel(self.master)
        data_merge_model.sec_init()
        data_merge_model.sub_view.create_layout_init()
        data_merge_model.sub_view.mainloop()
        pass

    def revenue_calculate(self):
        # 收益测算
        # 实例化model，model内置view实例化
        from module.addition_module.revenue_calculate.model import RevenueCalculateModel
        revenue_calculate_model = RevenueCalculateModel(self.master)
        revenue_calculate_model.sec_init()
        revenue_calculate_model.sub_view.create_layout_init()
        revenue_calculate_model.sub_view.mainloop()
        pass

    def order_integration(self):
        # 实例化model，model内置view实例化
        from module.addition_module.order_integration.model import OrderIntegrationModel
        order_integration_model = OrderIntegrationModel(self.master)
        order_integration_model.sec_init()
        order_integration_model.sub_view.create_layout_init()
        order_integration_model.sub_view.mainloop()

    def message_order(self):
        # 实例化model，model内置view实例化
        from module.addition_module.message_order.model import MessageOrderModel
        message_order_model = MessageOrderModel(self.master)
        message_order_model.sec_init()
        message_order_model.sub_view.create_layout_init()
        message_order_model.sub_view.mainloop()

    def backtesting_visualization(self):
        # 实例化model，model内置view实例化
        from module.addition_module.backtesting_visualization.model import BacktestingVisualizationModel
        backtesting_visualization_model = BacktestingVisualizationModel(self.master)
        backtesting_visualization_model.sec_init()
        backtesting_visualization_model.sub_view.create_layout_init()
        backtesting_visualization_model.sub_view.mainloop()

    def sector_guide(self):
        # 实例化model，model内置view实例化
        from module.addition_module.sector_guide.model import SectorGuideModel
        sector_guide_model = SectorGuideModel(self.master)
        sector_guide_model.sec_init()
        sector_guide_model.sub_view.create_layout_init()
        sector_guide_model.sub_view.mainloop()

    def comparison_chart(self):
        # 实例化model，model内置view实例化
        from module.addition_module.comparison_chart.model import ComparisonChartModel
        comparison_chart_model = ComparisonChartModel(self.master)
        comparison_chart_model.sec_init()
        comparison_chart_model.sub_view.create_layout_init()
        comparison_chart_model.sub_view.mainloop()
